package com.alpaak.hellospringboot;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Collections;

/**
 * @Description: 类描述 代码生成器
 * @author: alpaak
 * @date: 2021/12/22 15:30
 * @Since 1.0
 */
public class MybatisGenerator {
    // 本地项目路径 - 要改
    private final static String LOCAL_PATH = "D:\\3-study\\cundao\\hello-springboot";
    // xml包路径
    private final static String MAPPER_XML_PATH = "/src/main/resources/mapper";
    // 包路径
    private final static String PACKAGE_PATH = "/src/main/java";

    //项目包路径
    private final static String PACKAGE_NAME = "com.alpaak.hellospringboot";

    //作者
    private final static String AUTHOR = "alpaak";
    //数据库
    private final static String DATABASE = "cundao";
    //服务器地址
    private final static String DB_HOST = "127.0.0.1:3306";
    //帐号
    private final static String DB_USERNAME = "root";
    //密码
    private final static String DB_PASSWORD = "123456";

    //数据库表 - 通过表成成代码  - 要改
    private final static String[] TABLES = new String[]{
            "user"
    };
    public static void main(String[] args) {
        String dbUrl = "jdbc:mysql://" + DB_HOST + "/" + DATABASE + "?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false";
        FastAutoGenerator.create(dbUrl,DB_USERNAME ,DB_PASSWORD)
                .globalConfig(builder -> {
                    builder.author(AUTHOR) // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(LOCAL_PATH + PACKAGE_PATH); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent(PACKAGE_NAME) // 设置父包名
                            .moduleName("") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, LOCAL_PATH + MAPPER_XML_PATH)); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude(TABLES); // 设置需要生成的表名
//                            .addTablePrefix("t_", "c_"); // 设置过滤表前缀
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();

    }
}
